package com.spring.component.exception;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Getter;
import lombok.Setter;

/**
 * @author fanfever
 * @email fanfeveryahoo@gmail.com
 * @url https://github.com/fanfever
 * @date 2016年7月9日
 */
@Getter
@Setter
@JsonIgnoreProperties({ "cause", "stackTrace", "localizedMessage", "suppressed" })
public class BadRequestException extends BaseException {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	@JsonProperty("message")
	private List<String> messageList;
	private I18nMessage i18nMessage;

	private Integer code;

	public BadRequestException(String message) {
		i18nMessage = new I18nMessage(message);
		this.type = ExceptionType.ERROR;
		this.messageList = new ArrayList<>();
		this.messageList.add(message);
	}

	public BadRequestException(String message, String... param) {
		i18nMessage = new I18nMessage(message, param);
		this.type = ExceptionType.ERROR;
		this.messageList = new ArrayList<>();
		this.messageList.add(message);
	}

	public BadRequestException(ExceptionType exceptionType, List<String> messageList) {
		this.type = exceptionType;
		this.messageList = messageList;
	}

	@Override
	public String getMessage() {
		StringBuilder sb = new StringBuilder();
		if (CollectionUtils.isEmpty(this.messageList)) {
			return "No message!";
		}
		if (this.messageList.size() == 1) {
			return this.messageList.get(0);
		} else {
			for (int i = 0; i < this.messageList.size(); i++) {
				sb.append(i + 1).append(". ").append(this.messageList.get(i)).append("\n");
			}
		}
		return sb.toString();
	}
}
